package com.tianqu.tms.www.user.controller;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.entity.UserAuth;
import com.tianqu.tms.entity.UserRole;
import com.tianqu.tms.www.user.viewhelper.UserDetailViewHelper;

/**
 * @author zhangxueliang 用户角色修改录入
 */
public class UserRoleUpdateInputController extends TmsController {
	private Logger logger = Logger
			.getLogger(UserRoleUpdateInputController.class);
	public static final String ROLELIST = "roleList";

	@SuppressWarnings("unchecked")
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");

		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			UserDetailViewHelper userDetailViewHelper = new UserDetailViewHelper();
			userDetailViewHelper = (UserDetailViewHelper) uc
					.getAttribute("view");
			String id = (String) uc.getAttribute("id");

			List roleList = new ArrayList();
			int ucId = Integer.parseInt(userDetailViewHelper.getUcId());
			UserRole userRole = UserRole.getInstance();
			UserAuth userAuth = UserAuth.getInstance();

			List<UserAuth> userAuthList = userAuth.getAuth(Integer.parseInt(id));
			roleList = userRole.getUsedRole(userAuthList, ucId);
			uc.setAttribute(ROLELIST, roleList);

			logger
					.debug(this.getClass().getName()
							+ "#doAction was completed.");
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

	}

}
